home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / FROM.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  11KB  |  378 lines

  1. /*
  2. **  FROM.C
  3. **
  4. **  POP3 client, uses ASYNC functions.
  5. **
  6. **  Note that this program reads FROM.INI.
  7. **
  8. */
  9.  
  10. #include <windows.h>
  11. #include <winsock.h>
  12.  
  13. #include "wil.h"
  14. #include "message.h"
  15. #include "paint.h"
  16. #include "about.h"
  17. #include "async.h"
  18. #include "readini.h"
  19. #include "str.h"
  20.  
  21. #ifdef WIN32
  22. #define USE_INS HINSTANCE
  23. #define USE_PTR PSTR
  24. #else
  25. #define USE_INS HANDLE
  26. #define USE_PTR LPSTR
  27. #endif
  28.  
  29. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  30.  
  31. /* globals */
  32.  
  33. HWND hMainWnd;            /* main window handle */
  34.  
  35. #define BS            8
  36. #define LF           10
  37. #define CR           13
  38. #define ESC          27
  39.  
  40. #define MAX_BUF     512
  41. #define STR_SIZE     50
  42.  
  43. #define POP3_PORT   110
  44.  
  45. #define FROM_CONNECT   101
  46. #define FROM_CONN_OK   102
  47. #define FROM_USER_OK   103
  48. #define FROM_PASS_OK   104
  49. #define FROM_STAT_OK   105
  50. #define FROM_LOOP      106
  51. #define FROM_TOP_OK    107
  52. #define FROM_READ_OK   108
  53. #define FROM_QUIT      109
  54. #define FROM_QUIT_OK   110
  55. #define FROM_FAILURE   111
  56.  
  57. static USE_INS hInstance;
  58. static int WinWidth = 8 * NCOLS;  /* window width */
  59. static int WinHeight = 15 * NROWS;/* window height */
  60. static char Temp[MAX_BUF+8];      /* temprary buffer */
  61. static SOCKET Socket = 0;         /* socket */
  62. static int EchoFlag = 1;          /* echo commands of TRUE */
  63. static HCURSOR ArrowCursor;       /* arrow cursor */
  64. static HCURSOR WaitCursor;        /* hour glass cursor */
  65. static LPSTR BufferPtr;           /* pointer to buffer in ASYNC */
  66. static int MsgCount;
  67. static int MsgIndex;
  68.  
  69. static char Pop3String[STR_SIZE] = "";  /* POP3 server */
  70. static char UserString[STR_SIZE] = "";  /* POP3 user */
  71. static char PassString[STR_SIZE] = "";  /* POP3 password */
  72.  
  73. /* display error message */
  74.  
  75. static void DisplayError(int Code, LPSTR Msg)
  76. {DisplayString("ERROR: ");
  77.  if(Msg) DisplayString(Msg);
  78.  if(Code)
  79.    {wilErrorText(Code,(LPSTR)Temp,50);
  80.     DisplayLine((LPSTR)Temp);
  81.    }
  82.  /* restore arrow cursor */
  83.  SetCursor(ArrowCursor);
  84. }
  85.  
  86. /* WinMain */
  87.  
  88. #ifdef WIN32
  89. int WINAPI
  90. #else
  91. int PASCAL
  92. #endif
  93. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  94.         USE_PTR szCmdLine,  int nCmdShow)
  95. {WNDCLASS  wc;
  96.  MSG msg;
  97.  BOOL Result;
  98.  if(!hPrevInstance)
  99.    {/* register main window class */
  100.     wc.style = CS_HREDRAW | CS_VREDRAW;
  101.     wc.lpfnWndProc = MainWndProc;
  102.     wc.cbClsExtra = 0;
  103.     wc.cbWndExtra = 0;
  104.     wc.hInstance = hInst;
  105.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  106.     wc.hCursor = NULL;
  107.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  108.     wc.lpszMenuName =  "HostMenu";
  109.     wc.lpszClassName = "HostWClass";
  110.     Result = RegisterClass(&wc);
  111.     if(!Result) return FALSE;
  112.    }
  113.  
  114.  /* create main window */
  115.  hInstance = hInst;
  116.  hMainWnd = CreateWindow(
  117.         "HostWClass",   "From",    WS_OVERLAPPEDWINDOW,
  118.         CW_USEDEFAULT,  CW_USEDEFAULT,
  119.         WinWidth,       WinHeight,
  120.         NULL,           NULL,
  121.         hInstance,      NULL);
  122.  ShowWindow(hMainWnd, nCmdShow);
  123.  UpdateWindow(hMainWnd);
  124.  
  125.  /* window control loop */
  126.  
  127.  while(GetMessage(&msg,NULL,0,0))
  128.    {
  129.     TranslateMessage(&msg);
  130.     DispatchMessage(&msg);
  131.    }
  132.  return (msg.wParam);
  133. } /* end WinMain */
  134.  
  135. #ifdef WIN32
  136. LRESULT CALLBACK
  137. #else
  138. long FAR PASCAL
  139. #endif
  140. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  141. {int n, Code;
  142.  HDC hDC;
  143.  PAINTSTRUCT ps;
  144.  static char LastChar;
  145. #ifdef WIN32
  146. #else
  147.  static FARPROC lpfnAboutDlgProc;
  148. #endif
  149.  hMainWnd = hWindow;
  150.  switch (iMsg)
  151.     {case WM_CREATE:
  152.       /* create cursors */
  153.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  154.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  155.       SetCursor(ArrowCursor);
  156. #ifdef WIN32
  157. #else
  158.        /* create thunk for Win16 */
  159.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  160. #endif
  161.       /* initialize paint module */
  162.       PaintInit();
  163.       /* attach WINSOCK */
  164.       DisplayString("Attaching WINSOCK...");
  165.       Code = wilAttach();
  166.       DisplayLine("OK");
  167.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  168.       else
  169.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  170.          DisplayLine((LPSTR)Temp);
  171.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  172.          DisplayLine((LPSTR)Temp);
  173.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  174.          DisplayLine((LPSTR)Temp);
  175.         }
  176.       /* open FROM.INI file */
  177.       *Temp = '\0';
  178.       if(!IniOpen("FROM.INI")) break;
  179.       DisplayLine("FROM.INI opened");
  180.       while(1)
  181.         {/* read next line from FROM.INI */
  182.          if(IniRead((LPSTR)Temp)<0) break;
  183.             /*DisplayLine((LPSTR)Temp);*/
  184.          /* test for all legal keywords */
  185.          IniExtract((LPSTR)Temp,"POP3",(LPSTR)Pop3String);
  186.          IniExtract((LPSTR)Temp,"USER",(LPSTR)UserString);
  187.          IniExtract((LPSTR)Temp,"PASS",(LPSTR)PassString);
  188.         }
  189.       /* verify that we have all strings read in */
  190.       if(lstrlen((LPSTR)Pop3String)==0) DisplayLine("Missing POP3 server name.");
  191.       if(lstrlen((LPSTR)UserString)==0) DisplayLine("Missing POP3 USER name");
  192.       if(lstrlen((LPSTR)PassString)==0) DisplayLine("Missing POP3 Password");
  193.       break;
  194.  
  195.      case WM_COMMAND:
  196.          switch(wParam)
  197.            {
  198.             case MSG_ABOUT :
  199. #ifdef WIN32
  200.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  201. #else
  202.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  203. #endif
  204.                return 0;
  205.  
  206.             case MSG_DEBUG:
  207.               wsprintf((LPSTR)Temp,"DEBUG: Pop3String=[%s]", (LPSTR)Pop3String);
  208.               DisplayLine((LPSTR)Temp);
  209.               wsprintf((LPSTR)Temp,"DEBUG: UserString=[%s]", (LPSTR)UserString);
  210.               DisplayLine((LPSTR)Temp);
  211.               wsprintf((LPSTR)Temp,"DEBUG: PassString=[%s]", (LPSTR)PassString);
  212.               DisplayLine((LPSTR)Temp);
  213.               break;
  214.  
  215.             case MSG_ECHO:
  216.               EchoFlag = 1 - EchoFlag;
  217.               DisplayString("Echo flag is now ");
  218.               if(EchoFlag) DisplayLine("ON");
  219.               else DisplayLine("OFF");
  220.               break;
  221.  
  222.             case MSG_BREAK:
  223.               SetCursor(ArrowCursor);
  224.               wilCancelBlocking();
  225.               wilCloseSocket(Socket);
  226.               break;
  227.  
  228.             case MSG_EXIT:
  229.               wilRelease();
  230.               DestroyWindow(hMainWnd);
  231.               break;
  232.  
  233.             case MSG_MAIL_CHK:
  234.               POST_MSG(FROM_CONNECT);
  235.               break;
  236.  
  237.            }
  238.          break;
  239.  
  240.     case WM_USER: /* posted by WIL */
  241.       AsyncProcessMsg(lParam);
  242.       break;
  243.  
  244.     case WM_USER+1:  /* posted by Async functions */
  245.       /* test response code [+OK maps to 100, -ERR maps to 500] */
  246.       if(lParam>=500)
  247.         {/* POP3/SMTP server returns fatal error code */
  248.          POST_MSG(FROM_FAILURE);
  249.          break;
  250.         }
  251.       /* execute case */
  252.       switch(wParam)
  253.         {
  254.          case FROM_CONNECT:
  255.            /* log onto POP3 server */
  256.            SetCursor(WaitCursor);
  257.            AsyncSetEcho(FALSE);
  258.            Socket = AsyncConnect(hMainWnd,"POP3",(LPSTR)Pop3String, POP3_PORT,
  259.                        FROM_CONN_OK, FROM_FAILURE, ASYNC_PLUS_MINUS);
  260.            break;
  261.  
  262.          case FROM_CONN_OK:
  263.            /* send USER command */
  264.            wsprintf((LPSTR)Temp,"USER %s", (LPSTR)UserString);
  265.            AsyncCommand((LPSTR)Temp, FROM_USER_OK,
  266.                        FROM_FAILURE, ASYNC_PLUS_MINUS);
  267.            break;
  268.  
  269.          case FROM_USER_OK:
  270.            /* send PASS (password) command */
  271.            wsprintf((LPSTR)Temp,"PASS %s", (LPSTR)PassString);
  272.            AsyncCommand((LPSTR)Temp, FROM_PASS_OK,
  273.                        FROM_FAILURE, ASYNC_PLUS_MINUS);
  274.            break;
  275.  
  276.          case FROM_PASS_OK:
  277.            /* send STAT (statistics) command */
  278.            AsyncCommand("STAT", FROM_STAT_OK,
  279.                   FROM_FAILURE, ASYNC_PLUS_MINUS);
  280.            break;
  281.  
  282.          case FROM_STAT_OK:
  283.            /* get the message count */
  284.            BufferPtr = AsyncGetBufPtr();
  285.            MsgCount = (int) wilParseDecimal(BufferPtr+4);
  286.            wsprintf((LPSTR)Temp,"  %d messages waiting",MsgCount);
  287.            DisplayLine((LPSTR)Temp);
  288.            /* look at each message in turn */
  289.            MsgIndex = 0;
  290.            POST_MSG(FROM_LOOP);
  291.            break;
  292.  
  293.          case FROM_LOOP:
  294.            /* is there another header ? */
  295.            MsgIndex++;
  296.            if(MsgIndex>MsgCount)
  297.              {/* no more headers, so sign off POP server */
  298.               POST_MSG(FROM_QUIT);
  299.               break;
  300.              }
  301.            /* get header for message <MsgIndex> */
  302.            wsprintf((LPSTR)Temp,"TOP %d 0", MsgIndex);
  303.            AsyncCommand((LPSTR)Temp, FROM_TOP_OK,
  304.                        FROM_FAILURE, ASYNC_PLUS_MINUS);
  305.            break;
  306.  
  307.          case FROM_TOP_OK:
  308.            /* display header info */
  309.            wsprintf((LPSTR)Temp,"Msg %d: ",MsgIndex);
  310.            DisplayString((LPSTR)Temp);
  311.            /* read 1st line of header info */
  312.            LastChar = '\n';
  313.            AsyncRead(FROM_READ_OK, FROM_FAILURE, ASYNC_SINGLE_LINE);
  314.            break;
  315.  
  316.          case FROM_READ_OK:
  317.            /* read was successfull */
  318.            BufferPtr = AsyncGetBufPtr();
  319.            /* check if this is the last line */
  320.            if((LastChar=='\n')&&(*BufferPtr=='.')&&(*(BufferPtr+1)=='\r') )
  321.              {/* end of this header message */
  322.               DisplayLine("");
  323.               POST_MSG(FROM_LOOP);
  324.               break;
  325.              }
  326.            /* got good header line */
  327.            n = lstrlen((LPSTR)BufferPtr);
  328.            LastChar = *(BufferPtr+n-1);
  329.            /* look for "From" or "Subject" */
  330.            Code = IsLeftString(BufferPtr,(LPSTR)"From");
  331.            if(!Code) Code = IsLeftString(BufferPtr,(LPSTR)"Subject");
  332.            if(Code)
  333.              {StripCRLF(BufferPtr);
  334.               wsprintf((LPSTR)Temp,"%s ", BufferPtr);
  335.               DisplayString((LPSTR)Temp);
  336.              }
  337.            /* read next header line */
  338.            AsyncRead(FROM_READ_OK, FROM_FAILURE, ASYNC_SINGLE_LINE);
  339.            break;
  340.  
  341.          case FROM_QUIT:
  342.            /* send QUIT command */
  343.            AsyncCommand("QUIT", FROM_QUIT_OK,
  344.                   FROM_FAILURE, ASYNC_PLUS_MINUS);
  345.            break;
  346.  
  347.          case FROM_FAILURE:
  348.            DisplayLine("POP3 command fails.");
  349.            /* fall thru... */
  350.  
  351.          case FROM_QUIT_OK:
  352.            /* all done */
  353.            wilCloseSocket(Socket);
  354.            SetCursor(ArrowCursor);
  355.            break;
  356.  
  357.         } /* end-switch(wParam) */
  358.  
  359.     case WM_PAINT:
  360.       HideCaret(hMainWnd);
  361.       hDC = BeginPaint(hMainWnd, &ps);
  362.       SetMapMode(hDC,MM_ANISOTROPIC);
  363.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  364.       PaintMain(hDC,&ps);
  365.       EndPaint(hMainWnd,&ps);
  366.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  367.       ShowCaret(hMainWnd);
  368.       break;
  369.  
  370.     case WM_DESTROY:
  371.       PostQuitMessage(0);
  372.       break;
  373.  
  374.     default:
  375.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  376.    }
  377.  return 0;
  378. } /* end MainWndProc */